package com.wzl.customviewproject.util;

import android.content.Context;
import android.util.DisplayMetrics;

import androidx.recyclerview.widget.LinearSmoothScroller;

/**
 * 作者：longxiansen
 * 功能：
 */
public class CenterSmoothScroll extends LinearSmoothScroller {
    public CenterSmoothScroll(Context context) {
        super(context);
    }

    /**
     *  recyclerview的中心点和item的中心点的相差值即item需要滑动的距离和方向
     * @param viewStart
     * @param viewEnd
     * @param boxStart
     * @param boxEnd
     * @param snapPreference
     * @return
     */
    @Override
    public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
//        return super.calculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, snapPreference);
        return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
    }

    //计算每个像素滑动的位置
    @Override
    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
//        return super.calculateSpeedPerPixel(displayMetrics);
        return 100f/displayMetrics.densityDpi;
    }
}
